package cn.leonis.leetcode;

/**
 * @Author Tobi
 * @Date 2021-07-14 下午1:24
 * 寻找峰值，峰值的元素有个特点，一定比周围的元素都大
 */
public class FindPeakElementSolution {

    public static void main(String[] args) {
        int[] nums = new int[]{1, 2, 1, 3, 5, 6, 4};
        FindPeakElementSolution solution = new FindPeakElementSolution();
        System.out.println(solution.findPeakElement(nums));
    }

    public int findPeakElement(int[] nums) {
        if (null == nums) {
            return -1;
        }
        if (nums.length == 1) {
            return 0;
        }
        int left = 0, right = nums.length - 1;
        while (left < right) {
            int mid = (right - left) / 2 + left;
            if (nums[mid] > nums[mid + 1]) {
                right = mid;
            }else {
                left = mid + 1;
            }
        }

        return left;
    }
}
